<template>
  <div>
    <el-dialog
      :title="showTitle"
      :visible="showDialog"
      @close="close"
    >
      <el-form ref="form" :model="formData" :rules="formRules" label-width="120px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" placeholder="2-10个字符" style="width:80%" size="mini" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formData.code" placeholder="2-10个字符" style="width:80%" size="mini" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="managerId">
          <el-select v-model="formData.managerId" placeholder="请选择负责人" style="width:80%" size="mini">
            <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce" :rows="4" style="width: 80%" type="textarea" size="mini" placeholder="1-100个字符" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="btnOk">确认</el-button>
              <el-button size="mini" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { addDepartment, getDepartment, getDetail, getManagerList, updateDetail } from '@/api/department'

export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    fatherId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      formData: {
        code: '', // 部门编码
        name: '', // 部门名称
        pid: '', // 父级id
        managerId: '', // 负责人id
        introduce: '' // 部门介绍
      },
      formRules: {
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { max: 10, min: 2, message: '部门编码长度应在2-10字符之间', trigger: 'blur' },
          { trigger: 'blur', validator: async(rule, value, callback) => {
            let res = await getDepartment()
            if (this.formData.id) {
              res = res.filter(item => item.id !== this.formData.id)
            }
            if (res.some(item => item.code === value)) { // 存在一项则为true
              callback(new Error('部门编码已存在'))
            } else {
              callback()
            }
          } }
        ],
        name: [{ required: true, message: '部门名字不能为空', trigger: 'blur' },
          { max: 10, min: 2, message: '部门名称长度应在2-10字符之间', trigger: 'blur' },
          { trigger: 'blur', validator: async(rule, value, callback) => {
            let res = await getDepartment()
            if (this.formData.id) {
              res = res.filter(item => item.id !== this.formData.id)
            }
            if (res.some(item => item.name === value)) { // 存在一项则为true
              callback(new Error('部门名称已存在'))
            } else {
              callback()
            }
          } }
        ],
        managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { max: 100, min: 1, message: '部门介绍应在1-100字符之间', trigger: 'blur' }
        ]
      },
      managerList: [] // 存储负责人列表
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManager()
  },
  methods: {
    close() {
      this.formData = {
        code: '', // 部门编码
        name: '', // 部门名称
        pid: '', // 父级id
        managerId: '', // 负责人id
        introduce: '' // 部门介绍
      }
      this.$refs.form.resetFields() // 只能重置模版中绑定的数据 而模版中没有绑定id
      this.$emit('update:showDialog', false)
    },
    async getManager() {
      this.managerList = await getManagerList()
    },
    btnOk() {
      this.$refs.form.validate(async isok => {
        if (isok) {
          let msg = '新增'
          if (this.formData.id) {
            msg = '更新'
            await updateDetail(this.formData)
          } else {
            await addDepartment({ ...this.formData, pid: this.fatherId })
          }
          this.$emit('updateDepartment')
          this.$message.success(`${msg}成功`)
          this.close()
        }
      })
    },
    async showDetail() {
      this.formData = await getDetail(this.fatherId)
    }
  }
}
</script>

<style lang="less" scoped>

</style>
